package routers

import (
	"ginblog/controller"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

//SetupRouter 设置路由
func SetupRouter() *gin.Engine {
	r := gin.Default()

	//设置session
	store := cookie.NewStore([]byte("testkey"))
	//设置session和登录验证的中间件
	r.Use(sessions.Sessions("sessionid", store))

	//设置静态文件
	r.Static("/static", "static")
	//设置模板文件
	r.LoadHTMLGlob("templates/*")

	r.GET("/", controller.IndexHTML)

	//博客
	blogGroup := r.Group("blog")
	{
		//用户验证中间件
		blogGroup.Use(controller.IsLogin)

		blogGroup.GET("/", controller.BlogIndex)

		//注册
		blogGroup.GET("/register", controller.GotoRegister)
		blogGroup.POST("/register", controller.CreateUser)

		//登录
		blogGroup.GET("/login", controller.GotoLogin)
		blogGroup.POST("/login", controller.Login)

		//退出
		blogGroup.GET("/userexit", controller.UserExit)

		//文章添加
		blogGroup.GET("/add", controller.GotoCreatArt)
		blogGroup.POST("/add", controller.CreateArticle)

		//获取文章
		blogGroup.GET("/get/:id", controller.GotoAArtcle)

		//更新文章
		blogGroup.GET("/update/:id", controller.GotoUpArtcle)
		blogGroup.POST("/update/:id", controller.UpArticle)

		//删除文章
		blogGroup.GET("/delete/:id", controller.DelArtcle)
	}

	return r
}
